
var sum;
var car ;
var user;
var div ;
var number ;
var car_bg ;

//加操作
function plus(jia){
    var c = jia.parent().find(".quantity").val();
    var modId = jia.parent().find(".pid").val();
    c=parseInt(c)+1;
    jia.parent().find(".quantity").val(c);
    //console.log(modId);
    console.log(c);
    $.ajax({
        url : "easybuy/cart/addCart",
        dataType : "json",
        data : {"id": modId,"quantity":1,"userName":user},
        type : "post",
        success : function (data) {

                if(data.result == "success"){
                    $.ajax({
                        url : "easybuy/cart/showItem",
                        dataType: "json",
                        type : "post",
                        data : {"userName" : user},
                        //async : false,
                        success : function (data) {
                            var th = "";
                            var sumprice = 0;
                            if(data.length==0){
                                car.html("您还未添加商品");
                            }
                            for(var i=0;i<data.length;i++){
                                th +="<li><div class='img'><a href=''><img src='../images/"+data[i].product.fileName+"' width=\"58\" height=\"58\" /></a></div>";
                                th +="<div class='name'><a href=''>"+data[i].product.name+"</a></div>";
                                th +="<div class='price'>"+accounting.formatMoney(data[i].product.price, "￥",1, ",", ".")+"</div>";
                                th +="<div class='productCost'>"+accounting.formatMoney(data[i].cost, "￥",1, ",", ".")+"</div>";
                                th +="<div style='margin-left: 80px;text-align: center;position: fixed;margin-top: 60px' class='manager'>" +
                                    "        <input type='hidden' class='pid' value='"+data[i].product.id+"'>\n" +
                                    "        <input type='button' value='+'  onclick='plus(jq(this))'>\n"+
                                    "        <input type='text' style='width: 40px;text-indent: 5px;' class='quantity' value='"+data[i].quantity+"' readonly>\n" +
                                    "        <input type='button' value='-' onclick='mins(jq(this))'></div></li>";
                                sumprice+=data[i].cost;

                            }
                            number.html(data.length);
                            car.html("");
                            car.append(th);
                            sum.html(accounting.formatMoney(sumprice, "￥",1, ",", "."));
                            //console.log(11);
                        },
                        error : function (XMLHttpRequest, textStatus, errorThrown) {
                            alert("出现错误");
                            console.log(XMLHttpRequest.status);
                            console.log(XMLHttpRequest.readyState);
                            console.log(textStatus);
                        }
                    });
                }
        },
        error : function (XMLHttpRequest, textStatus, errorThrown) {
            alert("出现错误");
            console.log(XMLHttpRequest.status);
            console.log(XMLHttpRequest.readyState);
            console.log(textStatus);
        }
    });

}
function mins(jian){
    var c = jian.parent().find(".quantity").val();
    var modId = jian.parent().find(".pid").val();
    console.log(modId);
    if(c==1){
        c=1;
    }else{
        c=parseInt(c)-1;
        jian.parent().find(".quantity").val(c);
        $.ajax({
            url : "easybuy/cart/addCart",
            dataType : "json",
            data : {"id": modId,"quantity":-1,"userName":user},
            type : "post",
            success : function (data) {
                if(data.result == "success"){
                    $.ajax({
                        url : "easybuy/cart/showItem",
                        dataType: "json",
                        type : "post",
                        data : {"userName" : user},
                        //async : false,
                        success : function (data) {
                            var th = "";
                            var sumprice = 0;
                            if(data.length==0){
                                car.html("您还未添加商品");
                            }
                            for(var i=0;i<data.length;i++){
                                th +="<li><div class='img'><a href=''><img src='../images/"+data[i].product.fileName+"' width=\"58\" height=\"58\" /></a></div>";
                                th +="<div class='name'><a href=''>"+data[i].product.name+"</a></div>";
                                th +="<div class='price'>"+accounting.formatMoney(data[i].product.price, "￥",1, ",", ".")+"</div>";
                                th +="<div class='productCost'>"+accounting.formatMoney(data[i].cost, "￥",1, ",", ".")+"</div>";
                                th +="<div style='margin-left: 80px;text-align: center;position: fixed;margin-top: 60px' class='manager'>" +
                                    "        <input type='hidden' class='pid' value='"+data[i].product.id+"'>\n" +
                                    "        <input type='button' value='+'  onclick='plus(jq(this))'>\n"+
                                    "        <input type='text' style='width: 40px;text-indent: 5px;' class='quantity' value='"+data[i].quantity+"' readonly>\n" +
                                    "        <input type='button' value='-' onclick='mins(jq(this))'></div></li>";
                                sumprice+=data[i].cost;

                            }
                            number.html(data.length);
                            car.html("");
                            car.append(th);
                            sum.html(accounting.formatMoney(sumprice, "￥",1, ",", "."));
                            //console.log(11);
                        },
                        error : function (XMLHttpRequest, textStatus, errorThrown) {
                            alert("出现错误");
                            console.log(XMLHttpRequest.status);
                            console.log(XMLHttpRequest.readyState);
                            console.log(textStatus);
                        }
                    });
                }
            },
            error : function (XMLHttpRequest, textStatus, errorThrown) {
                //alert("出现错误");
                console.log(XMLHttpRequest.status);
                console.log(XMLHttpRequest.readyState);
                console.log(textStatus);
            }
        });

    }
}

$(function () {
    car = $(".cars");
    user = localStorage.getItem("user");
    div = $(".un_login");
    sum = $("#sum");
    number = $("#number");
    car_bg = $(".car_bg");
    var button = "<div style='margin-left: 80px;text-align: center;position: fixed;margin-top: 60px' class='manager'>" +
        "        <input type='button' value='+' onclick='plus(jq(this))'>\n" +
        "        <input type='text' style='width: 40px;text-indent: 5px' class='quantity' value='1'>\n" +
        "        <input type='button' value='-' onclick='mins(jq(this))'></div>";

    show();
    if(user!=null){
        div.html(user+"的购物车");
    }else if(car.length==0){
        car.html("购物车空空如也");
    }else if(user==null){
        car_bg.html("");
        car_bg.html("请先登录再使用购物车&nbsp;<a href='Login.html'><font color='red'>登录</font></a>");

    }
    // $(".manager .calculate:even").click(function () {
    //     var c = $(this).parent().find(".quantity").val();
    //     var modId = $(this).parent().find(".pid").val();
    //     c=parseInt(c)+1;
    //     $(this).parent().find(".quantity").val(c);
    //     console.log(modId);
    //     console.log(c);
    //
    // });
    $(".carbg a:odd").bind("click",function () {
        var id = $(this).parent().find(".id").val();
        if(localStorage.length == 0) {
            if (confirm("请先登录")) {
                window.location.href = "Login.html";
                }
            }else {
                console.log(id);
                var quantity = 1;
                var userName = localStorage.getItem("user");
                $.ajax({
                    url : "easybuy/cart/addCart",
                    data : {"id":id,"quantity":quantity,"userName":userName},
                    dataType : "json",
                    type : "post",
                    success : function (data) {
                        if(data.result == "success"){
                            alert("添加成功");
                            show();
                        }else if(data.fail=="failed"){
                            alert("添加失败");
                        }

                    },
                    error : function (XMLHttpRequest, textStatus, errorThrown) {
                        alert("出现错误");
                        console.log(XMLHttpRequest.status);
                        console.log(XMLHttpRequest.readyState);
                        console.log(textStatus);
                    }

                });
            }
    });
    function show(){
        var userName = localStorage.getItem("user");
        // var car = $(".cars");
        $.ajax({
            url : "easybuy/cart/showItem",
            dataType: "json",
            type : "post",
            data : {"userName" : userName},
            success : function (data) {
                var th = "";
                var sumprice = 0;
                if(data.length==0){
                    car.html("您还未添加商品");
                }
                for(var i=0;i<data.length;i++){
                    th +="<li><div class='img'><a href=''><img src='../images/"+data[i].product.fileName+"' width=\"58\" height=\"58\" /></a></div>";
                    th +="<div class='name'><a href=''>"+data[i].product.name+"</a></div>";
                    th +="<div class='price'>"+accounting.formatMoney(data[i].product.price, "￥",1, ",", ".")+"</div>";
                    th +="<div class='productCost'>"+accounting.formatMoney(data[i].cost, "￥",1, ",", ".")+"</div>";
                    th +="<div style='margin-left: 80px;text-align: center;position: fixed;margin-top: 60px' class='manager'>" +
                        "        <input type='hidden' class='pid' value='"+data[i].product.id+"'>\n" +
                        "        <input type='button' value='+'  onclick='plus(jq(this))'>\n"+
                        "        <input type='text' style='width: 40px;text-indent: 5px;' class='quantity' value='"+data[i].quantity+"' readonly>\n" +
                        "        <input type='button' value='-' onclick='mins(jq(this))'></div></li>";
                    //th += button+"</li>";
                    sumprice+=data[i].cost;
                    //$(".quantity").val(data[i].quantity);
                }
                number.html(data.length);
                sum.html(accounting.formatMoney(sumprice, "￥",1, ",", "."));
                car.html("");
                car.append(th);
            },
            error : function (XMLHttpRequest, textStatus, errorThrown) {
                //alert("出现错误");
                console.log(XMLHttpRequest.status);
                console.log(XMLHttpRequest.readyState);
                console.log(textStatus);
            }
        });
    }

});



